﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_trackback : BoardSubPartBase
{
	private Boolean isAuthorized;

	private BoardThread CurrentThread;
	private TrackbackCollection CurrentTrackbackCollection;

	private void reloadCurrentTrackbackCollection()
	{
		this.CurrentTrackbackCollection = Trackback.GetTrackbackCollection(this.CurrentThread.ComponentID, this.CurrentThread.ThreadID);
	}



	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];
		this.CurrentTrackbackCollection = (TrackbackCollection)this.Page.Items["CurrentTrackbackCollection"];
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		this.isAuthorized = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || (base.CurrentUserInfo.ProviderUserKey == this.CurrentThread.ProviderUserKey);
		if (!Page.IsPostBack)
		{
			this.Trackback_Bind();
		}
	}

	protected void Trackback_Bind()
	{
		if (this.CurrentThread.IsDeleted || this.CurrentThread.IsBlocked)
		{
			this.Visible = false;
		}
		else
		{
			TrackbackUrlTextBox.Text = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.IndexOf("/", 8)) + VUtility.WebApplicationVirtualPath +
					String.Format("VWebTrackback.ashx/{0}/{1}", base.CurrentBoard.ComponentID, this.CurrentThread.ThreadID);

			TrackbackUrlCopyDescription.Visible = !Request.Browser.IsBrowser("ie");

			TrackbackRepeater.DataSource = this.CurrentTrackbackCollection;
			TrackbackRepeater.DataBind();

			if (this.CurrentTrackbackCollection != null)
				TrackbackPostCountLiteral.Text = String.Format(GetLocalResourceObject("TrackbackCountFormat").ToString(), this.CurrentTrackbackCollection.Count);
			else
				TrackbackPostCountLiteral.Text = String.Empty;
		}
	}

	protected void TrackbackRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Header)
		{
			((Control)e.Item.FindControl("RemoveAllButton")).Visible = this.isAuthorized;
		}

		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			((Control)e.Item.FindControl("RemoveButton")).Visible = this.isAuthorized;

			if(this.isAuthorized)
				((IButtonControl)e.Item.FindControl("RemoveButton")).CommandArgument = ((Trackback)e.Item.DataItem).TrackbackID;
		}
	}

	protected void TrackbackRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
	{
		if (this.isAuthorized)
		{
			if (e.CommandName == "Remove")
			{
				switch (Trackback.Remove(base.CurrentBoard.ComponentID, e.CommandArgument.ToString()))
				{
					case TrackbackRemoveResult.Success:
						this.reloadCurrentTrackbackCollection();
						this.Trackback_Bind();
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Trackback_Do_Remove_Success"), true), true);
						break;
					case TrackbackRemoveResult.Failure:
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Trackback_Do_Remove_Failure"), true), true);
						break;
				}
			}
			else if (e.CommandName == "RemoveAll")
			{
				switch (Trackback.RemoveAll(base.CurrentBoard.ComponentID, this.CurrentThread.ThreadID))
				{
					case TrackbackRemoveResult.Success:
						this.reloadCurrentTrackbackCollection();
						this.Trackback_Bind();
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Trackback_Do_Remove_Success"), true), true);
						break;
					case TrackbackRemoveResult.Failure:
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Trackback_Do_Remove_Failure"), true), true);
						break;
				}
			}
		}
		else
		{
			ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
				VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Trackback_Do_Remove_Reject"), true), true);
		}
	}
}
